// 牌色
export const enum e_card_color {
    red = 1,
    black = 2,
    rect = 3,
    flower = 4,
}

//真实的颜色：红色，黑色
export const enum e_card_real_color {
    red = 1,
    black = 2,
}

// 牌值
export const enum e_card_score {
    c2 = 2,
    c3 = 3,
    c4 = 4,
    c5 = 5,
    c6 = 6,
    c7 = 7,
    c8 = 8,
    c9 = 9,
    c10 = 10,
    J = 11,
    Q = 12,
    K = 13,
    A = 14,
    // c2 = 16,    // 前后隔2，方便顺子判断
    kings = 18,
    kingb = 19,
}

// 牌型
export const enum e_card_arr_type {
    none = 0,           // 非法
    wang_zha = 1,       // 王炸
    zha = 2,            // 炸弹
    one = 3,            // 单张
    two = 4,            // 对子
    three = 5,          // 三张
    three1 = 6,         // 三带1
    three2 = 7,         // 三带2
    one_arr = 8,        // 单顺
    two_arr = 9,        // 双顺
    three_arr = 10,     // 三顺(飞机)
    three_arr_wing = 11,    // 飞机带翅膀
    four2 = 12,         // 四带2
}

export const enum e_role_type {
    ordinary = 0, // 普通玩家
    master = 1, // 地主
}

export interface I_card {
    id: number,
    color: e_card_color,
    score: e_card_score,
}

const initBreakPoints = new Map([
    [3, [75, 125, 150, 200]],
    [4, [105, 150, 180, 200]]
])

let breakPoints: any = {
    3: {
        'min': 45,
        'list': [75, 125, 150, 200]
    },
    4: {
        'min': 45,
        'list': [105, 150, 180, 200]
    }
}

export class cardUtil {

    private static res: I_cardRes = { "card_arr_type": e_card_arr_type.none, "cards": [], "score": e_card_score.c3 };


    /**
     * 
     * @param num 玩家数量
     */
    static getInitBreakPoint(num: any): number {
        
        return breakPoints[num]['list'][0]
    }

    /**
     * 
     * @param num 玩家数量
     */
     static getMinBreakPoint(num: any): number {
        console.log("getMinBreakPoint" + num)
        return breakPoints[num]['min']
    }

    static findCards(cards: I_card[], ids: number[]): I_card[] {
        let cardArr: I_card[] = [];
        for (let id of ids) {
            let find = false;
            for (let one of cards) {
                if (one.id === id) {
                    cardArr.push(one);
                    find = true;
                    break;
                }
            }
        }

        return cardArr;
    }

    // 不是打上家，随便出牌，需要决定牌型
    static getCardRes(arr: I_card[], idle_player_has_not_master_card: boolean = true): I_cardRes {
        let len = arr.length;

        // if ()

        if (len === 1) {
            this.setRes(e_card_arr_type.one, arr, arr[0].score); // 单张
        } else if (len === 2) {
            if (arr[0].score === arr[1].score && arr[0].color === arr[1].color) {
                this.setRes(e_card_arr_type.two, arr, arr[0].score);        // 对子
            } else {
                this.setRes(e_card_arr_type.none);
            }
        } else if (len === 3) {
            if (this.isSameScore(arr)) {
                this.setRes(e_card_arr_type.three, arr, arr[0].score);      // 三张
            } else {
                this.setRes(e_card_arr_type.none);
            }
        } else if (len === 4) {
            if (this.isSameScore(arr)) {
                this.setRes(e_card_arr_type.zha, arr, arr[0].score);    // 炸弹
            } else if (this.isSameScore(arr.slice(1, 4))) {
                this.setRes(e_card_arr_type.three1, arr.reverse(), arr[0].score);   // 三带1
            } else if (this.isSameScore(arr.slice(0, 3))) {
                this.setRes(e_card_arr_type.three1, arr, arr[0].score);  // 三带1
            } else {
                this.setRes(e_card_arr_type.none);
            }
        } else if (len === 5) {
            if (this.is_one_arr(arr)) { // 单顺
            } else if (this.is_three2(arr)) {  // 三带2
            } else {
                this.setRes(e_card_arr_type.none);
            }
        } else {
            if (this.is_one_arr(arr)) {             // 单顺
            } else if (this.is_two_arr(arr)) {      // 双顺
            } else if (this.is_three_arr(arr)) {    // 三顺
            } else if (this.is_four2(arr)) {        // 四带2
            } else if (this.is_three_arr_wing(arr)) {   // 飞机带翅膀
            } else {
                this.setRes(e_card_arr_type.none);
            }
        }
        let tmpRes = this.res;
        this.res = { "card_arr_type": e_card_arr_type.none, "cards": [], "score": e_card_score.c3 }
        return tmpRes;
    }

    // 打上家牌，比较牌型
    static isCardType(arr: I_card[], lastType: e_card_arr_type) {
        switch (lastType) {
            case e_card_arr_type.one:                                   // 单张
                this.setRes(e_card_arr_type.one, arr, arr[0].score);
                break;

            case e_card_arr_type.two:                            // 对子
                if (arr[0].score === arr[1].score) {
                    this.setRes(e_card_arr_type.two, arr, arr[0].score);
                }
                break;

            case e_card_arr_type.three:                           // 三张
                if (this.isSameScore(arr)) {
                    this.setRes(e_card_arr_type.three, arr, arr[0].score);
                }
                break;

            case e_card_arr_type.three1:                               // 三带1
                if (this.isSameScore(arr.slice(1, 4))) {
                    this.setRes(e_card_arr_type.three1, arr.reverse(), arr[0].score);
                } else if (this.isSameScore(arr.slice(0, 3))) {
                    this.setRes(e_card_arr_type.three1, arr, arr[0].score);
                }
                break;

            case e_card_arr_type.three2:                             // 三带2
                this.is_three2(arr);
                break;

            case e_card_arr_type.one_arr:                           // 单顺
                this.is_one_arr(arr);
                break;

            case e_card_arr_type.two_arr:                            // 双顺
                this.is_two_arr(arr);
                break;

            case e_card_arr_type.three_arr:                              // 三顺（飞机）
                this.is_three_arr(arr);
                break;

            case e_card_arr_type.three_arr_wing:                              // 飞机带翅膀
                this.is_three_arr_wing(arr);
                break;

            case e_card_arr_type.four2:                              // 四带二
                this.is_four2(arr);
                break;

            default:
                break;
        }

        if (this.res.card_arr_type === e_card_arr_type.none) {
            this.setRes(e_card_arr_type.none);
        }
        let tmpRes = this.res;
        this.res = { "card_arr_type": e_card_arr_type.none, "cards": [], "score": e_card_score.c3 }
        return tmpRes;
    }

    private static setRes(card_arr_type: e_card_arr_type, cards: I_card[] = [], score: e_card_score = e_card_score.c3) {
        this.res.card_arr_type = card_arr_type;
        this.res.cards = cards;
        this.res.score = score;
    }


    /**
     * 判断2张牌是否相同
     * @param a 
     * @param b 
     * @returns 
     */
    public static equals(a: I_card, b: I_card): boolean {
        return a.score === b.score && a.color === b.color
    }

    public static is_pair(cards: I_card[]) {
         return (cards.length == 2 && this.equals(cards[0], cards[1]))
    }
    

    // 王炸
    public static isWangZha(arr: I_card[]) {
        if (arr.length !== 2) {
            return false;
        }
        let kingCount = 0;
        for (let one of arr) {
            if (one.score === e_card_score.kings || one.score === e_card_score.kingb) {
                kingCount++;
            } else {
                break;
            }
        }
        return kingCount === 2;
    }
    // 炸弹
    public static isZha(arr: I_card[]) {
        if (arr.length !== 4) {
            return false;
        }
        return this.isSameScore(arr);
    }

    // 牌组是不是分数相同
    private static isSameScore(arr: I_card[]) {
        let isSame = true;
        let score = arr[0].score;
        for (let i = arr.length - 1; i >= 1; i--) {
            if (arr[i].score !== score) {
                isSame = false;
                break;
            }
        }
        return isSame;
    }

    // 单顺
    private static is_one_arr(arr: I_card[]) {
        var len = arr.length;
        if (len < 5 || len > 12) {
            return false;
        }
        let is = true;
        for (let i = arr.length - 2; i >= 0; i--) {
            if (arr[i + 1].score - arr[i].score !== 1) {
                is = false;
                break;
            }
        }
        if (is) {
            this.setRes(e_card_arr_type.one_arr, arr, arr[0].score);
        }
        return is;
    }

    // 三带1对
    private static is_three2(arr: I_card[]) {
        if (this.isSameScore(arr.slice(0, 3)) && this.isSameScore(arr.slice(3, 5))) {
            this.setRes(e_card_arr_type.three2, arr, arr[0].score);
            return true;
        } else if (this.isSameScore(arr.slice(0, 2)) && this.isSameScore(arr.slice(2, 5))) {
            this.setRes(e_card_arr_type.three2, arr.reverse(), arr[0].score);
            return true;
        } else {
            return false;
        }
    }

    // 双顺
    private static is_two_arr(arr: I_card[]) {
        if (arr.length % 2 !== 0) {
            return false;
        }
        let is = true;
        for (let i = arr.length - 2; i >= 0; i -= 2) {
            if (arr[i].score !== arr[i + 1].score) {
                is = false;
                break;
            }
            if (i < arr.length - 2 && arr[i + 2].score - arr[i].score !== 1) {
                is = false;
                break;
            }
        }
        if (is) {
            this.setRes(e_card_arr_type.two_arr, arr, arr[0].score);
        }
        return is;
    }

    // 三顺
    private static is_three_arr(arr: I_card[], setRes = true) {
        if (arr.length % 3 !== 0) {
            return false;
        }
        let is = true;
        for (let i = arr.length - 3; i >= 0; i -= 3) {
            if (arr[i].score !== arr[i + 1].score || arr[i].score !== arr[i + 2].score) {
                is = false;
                break;
            }
            if (i < arr.length - 3 && arr[i + 3].score - arr[i].score !== 1) {
                is = false;
                break;
            }
        }
        if (is && setRes) {
            this.setRes(e_card_arr_type.three_arr, arr, arr[0].score);
        }
        return is;
    }

    // 四带2
    private static is_four2(arr: I_card[]) {
        if (arr.length === 6) {
            let tmpArr: I_card[];
            if (tmpArr = arr.slice(0, 4), this.isSameScore(tmpArr)) {
                tmpArr.push(arr[4], arr[5]);
                this.setRes(e_card_arr_type.four2, tmpArr, tmpArr[0].score);
                return true;
            } else if (tmpArr = arr.slice(1, 5), this.isSameScore(tmpArr)) {
                tmpArr.push(arr[0], arr[5]);
                this.setRes(e_card_arr_type.four2, tmpArr, tmpArr[0].score);
                return true;
            } else if (tmpArr = arr.slice(2, 6), this.isSameScore(tmpArr)) {
                tmpArr.push(arr[0], arr[1]);
                this.setRes(e_card_arr_type.four2, tmpArr, tmpArr[0].score);
                return true;
            }
        } else if (arr.length === 8) {
            let tmpArr: I_card[];
            if (tmpArr = arr.slice(0, 4), arr[4].score === arr[5].score && arr[6].score === arr[7].score && this.isSameScore(tmpArr)) {
                if (arr[4].score === arr[6].score) {    // 两个炸弹,取大的为4，小的拆为两个对子
                    arr.reverse();
                }
                this.setRes(e_card_arr_type.four2, arr, arr[0].score);
                return true;
            } else if (tmpArr = arr.slice(2, 6), arr[0].score === arr[1].score && arr[6].score === arr[7].score && this.isSameScore(tmpArr)) {
                tmpArr.push(arr[0], arr[1], arr[6], arr[7]);
                this.setRes(e_card_arr_type.four2, tmpArr, tmpArr[0].score);
                return true;
            } else if (tmpArr = arr.slice(4, 8), arr[0].score === arr[1].score && arr[2].score === arr[3].score && this.isSameScore(tmpArr)) {
                tmpArr.push(arr[0], arr[1], arr[2], arr[3]);
                this.setRes(e_card_arr_type.four2, tmpArr, tmpArr[0].score);
                return true;
            }
        }
        return false;
    }

    // 飞机带翅膀
    private static is_three_arr_wing(arr: I_card[]) {
        if (arr.length < 8) {
            return false;
        }
        let obj: { 1: I_card[], 2: I_card[], 3: I_card[] } = { "3": [], "2": [], "1": [] };
        for (let i = 0; i < arr.length;) {
            let count = 1;
            for (let j = i + 1; j < arr.length; j++) {
                if (arr[j].score === arr[i].score) {
                    count++;
                } else {
                    break;
                }
                if (count === 3) {
                    break;
                }
            }
            (obj as any)[count].push(...arr.slice(i, i + count));
            i += count;
        }
        let len1 = obj["1"].length;
        let len2 = obj["2"].length;
        let len3 = obj["3"].length;
        if (len3 < 2) {
            return false;
        }
        if (len1 === 0 && len2 > 0 && len2 / 2 === len3 / 3) {               // 翅膀为对子
            if (this.is_three_arr(obj["3"], false)) {
                let tmpArr = obj["3"].concat(obj["2"]);
                this.setRes(e_card_arr_type.three_arr_wing, tmpArr, tmpArr[0].score);
                return true;
            } else {
                return false;
            }
        }

        if (len3 / 3 < len1 + len2) {
            return false;
        } else if (len3 / 3 === len1 + len2) {    // 333444555 + 778
            if (this.is_three_arr(obj["3"], false)) {
                obj["2"] = obj["2"].concat(obj["1"]);
                obj["2"].sort((a, b) => {
                    return a.score > b.score ? 1 : -1;
                });
                let tmpArr = obj["3"].concat(obj["2"]);
                this.setRes(e_card_arr_type.three_arr_wing, tmpArr, tmpArr[0].score);
                return true;
            } else {
                return false;
            }
        }
        //检查牌型，需要拆分3个的，如： 333444555 + 888， 333444555666 + 8889
        let tmpLen3 = len3;
        while (tmpLen3 / 3 > len1 + len2 + len3 - tmpLen3) {
            tmpLen3 -= 3;
        }
        if (tmpLen3 / 3 !== len1 + len2 + len3 - tmpLen3) {
            return false;
        }
        obj["3"].reverse();
        let tmpIndex = 0;
        while (true) {
            if (tmpIndex + tmpLen3 > len3) {
                return false;
            }
            if (this.is_three_arr(obj["3"].slice(tmpIndex, tmpIndex + tmpLen3).reverse(), false)) {
                break;
            } else {
                tmpIndex += 3;
            }
        }
        let tmpArrOne = obj["3"].slice(0, tmpIndex).concat(obj["3"].slice(tmpIndex + tmpLen3), obj["2"], obj["1"]);
        tmpArrOne.sort((a, b) => {
            return a.score > b.score ? 1 : -1;
        });
        let tmpArr = obj["3"].slice(tmpIndex, tmpIndex + tmpLen3).reverse().concat(tmpArrOne);
        this.setRes(e_card_arr_type.three_arr_wing, tmpArr, tmpArr[0].score);
        return true;
    }

    /**
     * 
     * @param card 
     * 等级分为：3角、大鬼、小鬼、7、2、其他
     */
    private static level(card: I_card): number {
        let level = 10;
        if (card.color == e_card_color.rect && card.score == e_card_score.c3) return level;

        level--;
        if (card.score == e_card_score.kingb) return level;

        level--;
        if (card.score == e_card_score.kings) return level;

        level--;
        if (card.score == e_card_score.c7) return level;

        level--;
        if (card.score == e_card_score.c2) return level;

        level--;
        return level;
    }


    /**
     * 是否是当前局的主的花色
     * @param card 
     * @param color 
     * @returns 
     */
    private static is_current_color(card: I_card, color: e_card_color): boolean {
        return card.color == color;
    }

    /**
     * 
     * @param c1 
     * @param c2 
     * @returns 0|1|-1
     */
    static show_compare(c1: I_card, c2: I_card, current_color: e_card_color): number {
        if (c1.score == c2.score && c1.color == c2.color) return 0;

        let l1 = this.level(c1)
        let l2 = this.level(c2)

        if (l1 != l2) {
            return l1 - l2;
        }

        //等级相同
        if (c1.color == c2.color) {//同样花色
            return c1.score - c2.score//比较数字大小
        }

        //哪个是软主哪个大
        if (this.is_current_color(c1, current_color)) return 1;
        if (this.is_current_color(c2, current_color)) return -1;


        // export const enum e_card_color {
        //     red = 1,   红
        //     black = 2, 黑
        //     rect = 3,  红
        //     flower = 4,黑


        //     black = 2, 黑
        //     red = 1,   红
        //     rect = 3,  红
        //     flower = 4,黑

        //     rect = 3,  红
        //     red = 1,   红
        //     black = 2, 黑
        //     flower = 4,黑

        //     flower = 4, 黑
        //     red = 1,    红
        //     black = 2,  黑
        //     rect = 3,   红


        // }
        //比花色大小
        //

        //red 和 flower在前面 红黑顺序是正常的
        if (current_color == e_card_color.red || current_color == e_card_color.flower)
            return c1.color - c2.color;

        let color1 = c1.color
        let color2 = c2.color
        if (current_color == e_card_color.black) {
            // console.log(color1, color2)
            if (c1.color == e_card_color.rect) color1 = e_card_color.flower
            else if (c1.color == e_card_color.flower) color1 = e_card_color.rect

            // if (c1.color == e_card_color.red) color1 = e_card_color.flower
            // else if (c1.color == e_card_color.flower) color1 = e_card_color.red


            if (c2.color == e_card_color.rect) color2 = e_card_color.flower
            else if (c2.color == e_card_color.flower) color2 = e_card_color.rect

            // console.log(color1, color2)
            // console.log()

        }
        if (current_color == e_card_color.rect) {
            if (c1.color == e_card_color.red) color1 = e_card_color.black
            else if (c1.color == e_card_color.black) color1 = e_card_color.red

            if (c2.color == e_card_color.red) color2 = e_card_color.black
            else if (c2.color == e_card_color.black) color2 = e_card_color.red
        }

        return color1 - color2;
    }

    /**
     * 
     * @param c1 
     * @param c2 
     * @returns 0|1|-1
     */
    static greater(c1: I_card, c2: I_card, current_color: e_card_color): boolean {
        if (c1.score == c2.score && c1.color == c2.color) return false;

        let l1 = this.level(c1)
        let l2 = this.level(c2)

        if (l1 != l2) {
            return l1 > l2;
        }

        //等级相同
        if (c1.color == c2.color) {//同样花色
            return c1.score > c2.score//比较数字大小
        }

        if (this.is_current_color(c1, current_color)) return true;
        if (this.is_current_color(c2, current_color)) return false;

        //2个都不是主，不会比它大的
        return false;
    }


    /**
     * 大鬼是红心，小鬼是方块
     * @param card 
     * @returns 
     */
    static get_master_color(card: I_card): e_card_color {
        if (card.score == e_card_score.kingb) return e_card_color.red
        if (card.score == e_card_score.kings) return e_card_color.rect

        return card.color

    }


    /**
     * 牌的分数
     * @param card
     * @returns 
     */
    static get_card_point(card: I_card): number {
        let point = 0;
        let score = card.score

        if (score == e_card_score.K || score == e_card_score.c10)
            point = 10;
        if (score == e_card_score.c5)
            point = 5;

        return point;
    }

    /**
     * 牌的分数
     * @param card
     * @returns 
     */
    static get_cards_point(cards: I_card[]): number {
        let point = 0;
        cards.forEach(c => {
            point += this.get_card_point(c)
        });

        return point;
    }

    /**
     * 是否硬主 大于等于2的就是硬主
     * @param card 
     * @returns 
     */
    static is_hard_master(card: I_card): boolean {
        // console.log(this.level(card))
        // console.log(this.level({
        //     "color": e_card_color.red, "score": e_card_score.c2,
        //     id: 0
        // }))
        return this.level(card) >=
            this.level({
                "color": e_card_color.red, "score": e_card_score.c2,
                id: 0
            })
    }

    /**
     * 是否主，花色对的上 是硬主
     * @param card 
     * @returns 
     */
    static is_master(card: I_card, color: e_card_color): boolean {
        // console.log('is_master:')
        // console.log(card)
        if (this.is_hard_master(card)) return true;

        return card.color == color
    }


    /**
     * 生成牌
     * @param num 多少副
     * @returns 牌的数组
     */
    static genCards(num: number, shuffle: Boolean = false) {
        let arr: I_card[] = [];
        let id = 0;
        function push(color: e_card_color) {
            for (let i = 3; i <= 14; i++) {
                arr.push({ "id": id++, "color": color, "score": i });
            }
            arr.push({ "id": id++, "color": color, "score": e_card_score.c2 });
        }

        for (let i = 0; i < num; i++) {
            push(e_card_color.red);
            push(e_card_color.black);
            push(e_card_color.rect);
            push(e_card_color.flower);
            arr.push({ "id": id++, "color": e_card_color.red, "score": e_card_score.kings });
            arr.push({ "id": id++, "color": e_card_color.red, "score": e_card_score.kingb });
        }

        if (shuffle) {
            arr.sort(function() { return 0.5 - Math.random(); });
        }

        return arr;
    }

    /**
     * 
     * @param firstCardsList 
     * @param masterColor 
     * @param cardsInHand 
     * @param selectedCards 
     * @param wantSelectCard 
     * @returns 
     */
    static can_be_select(firstCardsList: I_card[], masterColor: e_card_color, cardsInHand: I_card[], selectedCards: I_card[], wantSelectCard: I_card): Boolean {
        let firstCardsNum = firstCardsList.length;
        let selectedCardsNum = selectedCards.length;
        let firstCardColor = firstCardsList[0].color;

        if (selectedCardsNum >= firstCardsNum) {
            //选太多牌了
            return false;
        }

        /**
         * 单当前轮出的是对子
         * 如果只有一个匹配
         *      选了一个了 第二个必须选剩下那个
         *      没选，随便选
         * 如果没有
         *      随便选
         */
        let suitableCards = this.get_suitable_cards(cardsInHand, masterColor, firstCardsList[0])
        // suitableCards.forEach(card => {
        //     console.log(cardUtil.toString(card))
        // })
        // console.log(suitableCards)
        //没有对应花色的牌，随便选
        if (suitableCards.length == 0) {
            return true;
        }

        if (this.is_pair(firstCardsList)) {
            // console.log('is_pair')
            //合适的牌只剩一个了
            if (suitableCards.length == 1) {
                //已经选了一张牌
                if (1 == selectedCardsNum) {
                    if (this.is_suitable_card(suitableCards, wantSelectCard)) {
                        return true;
                    }
                    return false;
                }
                //没选牌，随便选
                if (0 == selectedCardsNum) {
                    return true;
                }
                return false;
            }
        }

        //还有对应花色的牌，必须是在合适的卡里面的
        if (this.is_suitable_card(suitableCards, wantSelectCard)) return true;

        return false;
    }

    static get_master_cards(cards: I_card[], masterColor: e_card_color): I_card[] {
        // console.log("get_master_cards")
        let cs: I_card[] = [];
        cards.forEach(card => {
            if (this.is_master(card, masterColor)) cs.push(card);
        })
        return cs;
    }

    static get_color_cards(cards: I_card[], masterColor: e_card_color, color: e_card_color): I_card[] {
        let cs: I_card[] = [];
        cards.forEach(card => {
            if (!this.is_master(card, masterColor) && card.color == color)
                cs.push(card);
        })
        return cs;
    }

    /**
     * 选择合适的卡有几张
     * @param cards 
     * @param masterColor 
     * @param firstCard 
     * @returns 
     */
    static get_suitable_cards(cards: I_card[], masterColor: e_card_color, firstCard: I_card): I_card[] {
        if (this.is_master(firstCard, masterColor)) {
            let suitableCards = this.get_master_cards(cards, masterColor);
            return (suitableCards.length === 0) ? cards : suitableCards; //没有合适颜色的牌了，那就是所有牌都可以打
        }
        
        let suitableCards = this.get_color_cards(cards, masterColor, firstCard.color);
        return (suitableCards.length === 0) ? cards : suitableCards; //没有合适颜色的牌了，那就是所有牌都可以打
    }

    static is_suitable_card(suitableCards: I_card[], card: I_card): boolean {
        return suitableCards.find(c => c.id === card.id) ? true : false;
    }

    /**
     * 
     * @param cardsResults 
     * @param masterColor 
     * @returns 
     */
    static get_winner(cardsResults: I_card[][], masterColor: e_card_color) {
        let winner = 0
        let cards0 = cardsResults[0];
        let max_card = cards0[0];

        if (this.is_pair(cards0)) {
            // console.log('is_pair');
            for (let i = 1; i < cardsResults.length; i++) {
                if (this.is_pair(cardsResults[i])) {
                    let card = cardsResults[i][0]
                    if (this.greater(card, max_card, masterColor)) {
                        max_card = card;
                        winner = i
                    }
                }
            }

        } else if (cards0.length == 1) {
            // console.log(cards0[0])
            for (let i = 1; i < cardsResults.length; i++) {
                let card = cardsResults[i][0]
                // console.log(card)
                if (this.greater(card, max_card, masterColor)) {
                    // console.log("greater")
                    max_card = card;
                    winner = i
                }
            }
        }
        return winner;
    }

    static get_points(cardsResults: I_card[][]) {
        let point = 0
        cardsResults.forEach(cards => {
            point += this.get_cards_point(cards);
        })

        return point;
    }

    static select_card(cards: I_card[], masterColor: e_card_color, firstCard: I_card[]) {
        let suitableCards = this.get_suitable_cards(cards, masterColor, firstCard[0]);
        if (suitableCards.length > 0) {
            return suitableCards[0];
        }

        return cards[0];
    }


    /**
     * 所有能打的牌的组合
     */
    static cards_can_be_play(cards: I_card[]): I_card[][] {
        let res: I_card[][] = [];

        let cardMap: number[] = []

        // res = cards
        for (let i = 0; i < cards.length; i++) {

            let card = cards.find(c => {
                return this.equals(cards[i], c) && (cards[i].id != c.id)
            })
            // console.log('find card' + JSON.stringify(card))
            if (card) {
                res.push([cards[i], card])
            }
            // for (let j = 0; j < cards.length; j++) {
            //     if ((cards[i] != cards[j]) && this.equals(cards[i], cards[j])) {
            //         res.push([cards[i], cards[j]])
            //         cardMap[cards[i].id] = cards[i].id
            //     }
            // }

            res.push([cards[i]])
        }
        // cards.forEach(c1 => {
        //     cards.forEach(c2 => {
        //         if ((c1.id !== c2.id) && this.equals(c1, c2) && !cardMap[c1.id]) {
        //             res.push([c1, c2])
        //             cardMap[c1.id] = c1.id
        //         }
        //     })
        //     res.push([c1])
        // })


        res.sort((a, b) => {
            return a.length > b.length ? -1 : 1;
        })

        return res
    }

    static toString(card: I_card): string {
        return `${(card.color)}-${card.score}`
    }
}


export interface I_cardRes {
    card_arr_type: e_card_arr_type,
    cards: I_card[],
    score: e_card_score,
}


function isBigger(last: I_cardRes, cardArr: I_card[]) {
    if (cardUtil.isWangZha(cardArr)) {
        return true;
    } else if (cardUtil.isZha(cardArr)) {
        if (last.card_arr_type === e_card_arr_type.wang_zha) {
            return false;
        } else if (last.card_arr_type === e_card_arr_type.zha) {
            return cardArr[0].score > last.score;
        } else {
            return true;
        }
    } else if (last.card_arr_type === e_card_arr_type.wang_zha || last.card_arr_type === e_card_arr_type.zha) {
        return false;
    } else if (last.cards.length !== cardArr.length) {
        return false;
    } else {
        // let res = cardUtil.getCardRes(cardArr, last.card_arr_type);
    }
}
